<?php

namespace Game\Logic;

use \Common\Logic\BaseLogic;

class GameConfigLogic extends BaseLogic
{
    static $typeDic = array(
        "1" => "首页背景",
        "2" => "首页按钮",
        "3" =>"标题",
        "4"=>"跑马灯"
    );

    private $listModel;

    public function __construct()
    {
        parent::__construct();
        $this->listModel = D('GameConfig');
    }

    /**
     * 返回类型字典
     * @return array
     */
    public function getTypeDic()
    {
        return static::$typeDic;
    }

    /**
     * 根据条件统计
     * @param array $where
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->listModel->where($where)->count();
    }

    /**
     * 获取任务列表
     * @param int $page
     * @param int $size
     * @param $where
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where)
    {
        $list = $this->listModel->where($where)->page($page, $size)->order("id asc")->select();
        $total = $this->statistics($where);
        return static::$res->suc("成功", array(
            "list" => $list,
            "total" => $total
        ));
    }

    /**
     * 获取任务明细
     * @param $where
     * @return mixed
     */
    public function getInfo($where)
    {
        $info = $this->listModel->where($where)->find();
        return static::$res->suc("成功", $info);
    }

    /**
     * 添加任务
     * @param $userId
     * @param $phone
     * @param $data
     * @return mixed
     */
    public function addTask($data)
    {

        static $fields = array(
            "type" => array("empty" => "类型不能为空"),
            "title" => array("empty" => ""),
            "cover" => array("empty" => ""),
            "uri" => array("empty" => "")
        );

        $fData = array();
        foreach ($fields as $title => $vo) {
            $val = $data[$title];
            if (!empty($vo['empty'])) {
                if (empty($val)) {
                    return static::$res->err($vo['empty']);
                }
            }
            $fData[$title] = trim($val);
        }

        $fData['task_state'] = 1;

        $id = $this->listModel->add($fData);
        if (empty($id)) {
            return static::$res->err("数据库异常，添加失败");
        }
        return static::$res->suc("成功");
    }


    /**
     * 编辑任务
     * @param $userId
     * @param $id
     * @param $data
     * @return mixed
     */
    public function editTask($id, $data)
    {
        if (empty($id)) {
            return static::$res->err("任务ID不能为空");
        }

        static $fields = array(
            "type" => array("empty" => "类型不能为空"),
            "title" => array("empty" => ""),
            "cover" => array("empty" => ""),
            "uri" => array("empty" => "")
        );

        $fData = array();
        foreach ($fields as $title => $vo) {
            $val = $data[$title];
            if (!empty($vo['empty'])) {
                if (empty($val)) {
                    return static::$res->err($vo['empty']);
                }
            }
            $fData[$title] = trim($val);
        }

        $fData['update_user'] = $userId;
        $fData['update_time'] = date("Y-m-d H:i:s");

        $coum = $this->listModel->where(array(
            "id" => $id
        ))->save($fData);
        if (empty($coum)) {
            return static::$res->err("数据库异常，编辑失败");
        }
        return static::$res->suc("成功");
    }

    /**
     * 删除一个任务
     * @param $userId
     * @param $id
     * @param string $msg
     * @return mixed
     */
    public function delTask($id, $msg = "发布者删除")
    {
        if (empty($id)) {
            return static::$res->err("任务ID不能为空");
        }

        $data = array(
            // "task_state" => -1,
            // "end_time" => date('Y-m-d H:i:s'),
            // "update_time" => date('Y-m-d H:i:s'),
            // "del_msg" => $msg
        );

        $coum = $this->listModel->where(array(
            "id" => $id
        ))->save($data);
        if (empty($coum)) {
            return static::$res->err("数据库异常，删除失败");
        }
        return static::$res->suc("删除成功");
    }

}
